package as3.carcassone.tiles {
	import as3.carcassone.model.Model;
	import as3.carcassone.view.TestClickPoint;

	import carc.Meeple;

	import nl.ypmits.Setup;

	import com.greensock.TweenMax;
	import com.greensock.easing.Cubic;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;

	public class GameTileView {
		public var graphic : Sprite = new Sprite();
		public var bitmap : Bitmap = new Bitmap();
		public var clickPoints : Vector.<Sprite> = new Vector.<Sprite>();
		public var data : GameTileData;

		public function GameTileView( bmd : BitmapData, data : GameTileData )
		{
			this.data = data;
			
			bitmap.bitmapData = bmd.clone();
			bitmap.x = -125;
			bitmap.y = -125;
			graphic.addChild( bitmap );
		}




		public function addClickPoint( testClickPoint : TestClickPoint ) : void
		{
			var point : TestClickPoint = new TestClickPoint( testClickPoint.manPositionVO );
			point.addEventListener( MouseEvent.CLICK, addMan );

			clickPoints.push( point );
		}



		private function addMan( e : MouseEvent ) : void
		{
			e.stopImmediatePropagation();
			data.currentManPosition = TestClickPoint( e.currentTarget ).manPositionVO;

			removeAllClickPoints();

			drawMan();

			lock();
		}



		private function drawMan() : void
		{
			var m : Meeple = new Meeple();
			m.x = data.currentManPosition.point.x;
			m.y = data.currentManPosition.point.y;
			m.filters = [ new DropShadowFilter( 4, 45, 0, .4, 6, 6, 1, 3 ) ];
			m.rotation = -data.rotation;
			graphic.addChild( m );
		}



		public function removeAllClickPoints() : void
		{
			for (var i : int = 0; i < clickPoints.length; i++) {
				clickPoints[i].parent.removeChild( clickPoints[i] as DisplayObject );
			}
			clickPoints = null;
		}



		public function lock() : void
		{
			graphic.mouseEnabled = false;
			graphic.mouseChildren = false;
			
			graphic.removeEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
			graphic.removeEventListener( Event.ENTER_FRAME, dragEnterHandler );
			Setup.stage.removeEventListener( MouseEvent.MOUSE_UP, mouseUpHandler );
			deactivateMouseWheel();
		}



		public function unlock() : void
		{
			trace("GameTileView.unlock("+[]+")");
			graphic.mouseEnabled = true;
			graphic.mouseChildren = true;
			graphic.addEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
		}



		private function activateMouseWheel() : void
		{
			graphic.addEventListener( MouseEvent.MOUSE_WHEEL, wheelHandler );
		}


		private function deactivateMouseWheel() : void
		{
			graphic.removeEventListener( MouseEvent.MOUSE_WHEEL, wheelHandler );
		}



		private function wheelHandler( e : MouseEvent ) : void
		{
			if ( e.delta > 0 )
			{
				data.posRotate = true;
			} else if ( e.delta < 0 )
			{
				data.posRotate = false;
			}
			data.rotate();
			deactivateMouseWheel();
			TweenMax.to( graphic, .25, { shortRotation:{ rotation:data.rotation }, ease:Cubic.easeInOut, onComplete:activateMouseWheel } );
		}



		private function mouseDownHandler( e : MouseEvent ) : void
		{
			TweenMax.to( graphic, .3, { scaleX:.5, scaleY:.5, ease:Cubic.easeInOut, onComplete:startDragHandler } );
		}



		private function startDragHandler() : void
		{
			Model.instance.gameboard.addChild( graphic );
			graphic.addEventListener( Event.ENTER_FRAME, dragEnterHandler );

			Setup.stage.addEventListener( MouseEvent.MOUSE_UP, mouseUpHandler );
		}



		private function mouseUpHandler( e : MouseEvent ) : void
		{
			graphic.removeEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
			graphic.removeEventListener( Event.ENTER_FRAME, dragEnterHandler );
			for (var i : int = 0; i < clickPoints.length; i++) {
				graphic.addChild( clickPoints[i] );
			}
			activateMouseWheel();
		}



		private function dragEnterHandler( e : Event ) : void
		{
			var w : Number = bitmap.width * .5;
			graphic.x = Setup.stage.mouseX - Setup.stage.mouseX % w + (w * .5);
			graphic.y = Setup.stage.mouseY - Setup.stage.mouseY % w + (w * .5);
		}
	}
}
